/*
package com.travel.websocket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.travel.dao.message.SingleMessageDAO;
import com.travel.dao.message.impl.SingleMessageDAOImpl;
import com.travel.pojo.message.SingleMessage;
import com.travel.utils.BeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@ServerEndpoint("/singlewebsocket/{userId}")
@Component
public class SingleWebSocket {

    private SingleMessageDAO SingleMessageDAO;

    //以用户的id为key，SingleWebSocket为对象保存起来
    private static Map<Long, SingleWebSocket> clients = new ConcurrentHashMap<Long, SingleWebSocket>();

    //日志
    private Logger logger= LoggerFactory.getLogger(this.getClass());

    //会话
    private Session session;

    //用户id
    private Long userId;

    */
/**
     * websocket不能直接注入
     * 需要配置，见BeanUtil
     *//*

    public void init(){
        SingleMessageDAO = BeanUtil.getBean(SingleMessageDAOImpl.class,"singleMessageDAO");
    }

    */
/**
     * 建立连接
     *//*

    @OnOpen
    public void onOpen(@PathParam("userId") Long userId, Session session){
        init();
        this.userId = userId;
        this.session = session;
        clients.put(userId, this);
    }

    */
/**
     * 错误
     *//*

    @OnError
    public void onError(Session session, Throwable error) {
        this.session=session;
        error.printStackTrace();
        logger.info("服务端错误："+error.getMessage());
    }

    */
/**
     * 连接关闭
     *//*

    @OnClose
    public void onClose() {
        try {
            clients.remove(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    */
/**
     * 收到客服端的消息，
     * 将客服端发的消息解析出接收者消息toid和发送内容msg
     * @param message 客户端发的消息
     *//*

    @OnMessage
    public void onMessage(String message,Session session) throws IOException {
        this.session=session;
        JSONObject jsonObject = JSON.parseObject(message);
        Long toid = jsonObject.getLong("toid");
        String msg = jsonObject.getString("msg");
        sendMessageTo(msg, toid);

        SingleMessage message1 =new SingleMessage.Builder()
                .setFromId(userId)
                .setToid(toid)
                .setMsg(msg)
                .build();
        logger.info("成功发送一条单发消息");
        this.SingleMessageDAO.saveMessage(message1);
    }


    */
/**
     * 发送消息
     * @param message 消息
     * @param toid 接收者id
     *//*

    public synchronized void sendMessageTo(String message,@PathParam("toid") Long toid) throws IOException {
        for (SingleWebSocket item : clients.values()) {
            if (item.userId.equals(toid)) {
                synchronized(session) {
                    item.session.getBasicRemote().sendText(message);
                }
                break;
            }
        }
    }


}*/
